package com.aier.common.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.aier.common.common.Constant;
import com.aier.common.common.MessageSend_CXTS;
import com.aier.common.common.MessageSend_Mobile;
import com.aier.common.common.MessageSingletonClient2;
import com.aier.common.dao.ICommonDAO;
import com.aier.common.service.ISysPhoneMessageService;

@Component("sysPhoneMessageService")
public class SysPhoneMessageServiceImpl_J implements ISysPhoneMessageService{
    @Resource(name = "commonDao")
    private ICommonDAO commonDao;
    
    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }

    @Override
    /**
     * 畅行天下
     * 只发送验证类短信内容
     */
    public void insertSendPhoneMessage(String phone, String message) {
        Boolean flag = false;
        Timestamp ctime = new Timestamp(new Date().getTime());
        try {
            //带扩展码
            for(int i=0;i<Constant.num_3;i++){
            	String j = MessageSend_CXTS.sendMessage(message, phone);
                if(j.equals("SUCCESS")){
                    flag = true;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(flag){
            commonDao.update("insert into phonemessage_send_history(phone,message,ctime) values(?,?,?)", 
                    new Object[]{phone,message,ctime});
        }else{
            commonDao.update("insert into phonemessage_sendError_history(phone,message,ctime) values(?,?,?)", new Object[]{phone,message,ctime});
            System.out.println("发送短信失败-手机号:"+phone+",内容:"+message);
        }

    }

    @Override
    /**
     * 亿美
     * 只发送 验证码
     */
    public void insertSendPhoneMessage2(String phone, String message) {
        Boolean flag = false;
        Timestamp ctime = new Timestamp(new Date().getTime());
        try {
            //带扩展码
            for(int i=0;i<Constant.num_3;i++){
                int j = MessageSingletonClient2.getClient().sendSMS(new String[]{phone}, message,"",1);
                if(j==0){
                    flag = true;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(flag){
            commonDao.update("insert into phonemessage_send_history(phone,message,ctime) values(?,?,?)", 
                    new Object[]{phone,message,ctime});
        }else{
            commonDao.update("insert into phonemessage_sendError_history(phone,message,ctime) values(?,?,?)", new Object[]{phone,message,ctime});
            System.out.println("发送短信失败-手机号:"+phone+",内容:"+message);
        }

    }
    
    @Override
    public void insertGetPhoneMessage(String phone, String message) {
        Timestamp ctime = new Timestamp(new Date().getTime());
        commonDao.update("insert into phonemessage_get_history(phone,message,ctime) values(?,?,?)", new Object[]{phone,message,ctime});
    }

    /**
     * 畅行天下
     */
    @Override
    public boolean insertSchoolPhoneMessage(String phone, String message,
            Integer sid, Long uid,Integer type,Long relativeid) {
        
        Boolean flag = false;
        Timestamp ctime = new Timestamp(new Date().getTime());
        try {
            //带扩展码
            for(int i=0;i<Constant.num_3;i++){
            	String j = MessageSend_CXTS.sendMessage(message, phone);
                if(j.equals("SUCCESS")){
                    flag = true;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(flag){
//            commonDao.update("insert into phonemessage_send_history(phone,message,ctime) values(?,?,?)", 
//                    new Object[]{phone,message,ctime});
            commonDao.update("insert into school_phonemessage_history(phone,message,sid,uid,ctime,type,relativeid) values(?,?,?,?,?,?,?)", 
                    new Object[]{phone,message,sid,uid,ctime,type,relativeid});
        }else{
            commonDao.update("insert into phonemessage_sendError_history(phone,message,sid,uid,ctime,type,relativeid) values(?,?,?,?,?,?,?)", 
                    new Object[]{phone,message,sid,uid,ctime,type,relativeid});
            System.out.println("发送短信失败-手机号:"+phone+",内容:"+message);
        }
        return flag;
    }
    
    /**
     * 网信通
     */
    @Override
    public boolean insertSchoolPhoneMessageMobile(String phone, String message,
            Integer sid, Long uid,Integer type,Long relativeid) {
        
        Boolean flag = false;
        Timestamp ctime = new Timestamp(new Date().getTime());
        
        String result = MessageSend_Mobile.sendMessage(message, phone);
        
        if ("0".equals(result.substring(0, 1)) && !"-1".equals(result.substring(result.indexOf("<Id>")+Constant.num_4, result.indexOf("</Id>")))) {
            flag = true;
        }
        if(flag){
            commonDao.update("insert into school_phonemessage_history(phone,message,sid,uid,ctime,type,relativeid) values(?,?,?,?,?,?,?)", 
                    new Object[]{phone,message,sid,uid,ctime,type,relativeid});
            System.out.println(ctime+"-发送短信成功-手机号:"+phone+",内容:"+message);
        }else{
            commonDao.update("insert into phonemessage_sendError_history(phone,message,sid,uid,ctime,type,relativeid) values(?,?,?,?,?,?,?)", 
                    new Object[]{phone,message,sid,uid,ctime,type,relativeid});
            System.out.println(ctime+"-发送短信失败-手机号:"+phone+",内容:"+message + "失败原因："+result);
        }
        return flag;
    }

    @Override
    public int countUserPhoneMessageNum(Long uid, Integer sid) {
        return commonDao.countByQuali(" from school_phonemessage_history where uid=? and sid=? and  " +
        		"MONTH(CURDATE())=MONTH(ctime) and YEAR(CURDATE())=YEAR(ctime);",
                new Object[]{uid,sid});
        
    }

    @Override
    public int countUserEdateNum(Long uid) {
        return commonDao.countByQuali(" from userapp_user_module where uid=? and CURRENT_DATE()<edate;",
                new Object[]{uid});
        
    }
    
    @Override
    public int searchSchoolBuyMethodBySid(Integer sid) {
        String method = (String)commonDao.listByObject("select method from school where sid = ?", new Object[]{sid},
                new RowMapper<String>() {
                @Override
                public String mapRow(ResultSet rs, int arg1)
                    throws SQLException {
                    return rs.getString("method");
                }
            }
        );
        if (null == method || "".equals(method)) {
            return 1;
        } else {
            return Integer.valueOf(method);
        }
    }

    @Override
    public int updateSchoolMessageCount(Integer sid) {
        return commonDao.update("UPDATE school s SET s.mesCount = s.mesCount-1 WHERE s.sid=?",
                new Object[] {sid});
    }

    @Override
    public int updateTeacherMessageCount(Long tid, Integer size) {
        return commonDao.update("UPDATE school_teacher st SET st.mesCount = st.mesCount-? WHERE st.tid=?",
                new Object[] {size,tid});
    }
    
}
